import { Utils } from './../../common/utils';
import { LoadingUtil } from './../../common/loading';
import { UiserviceProvider } from './../../providers/uiservice/uiservice';
import { BusiserviceProvider } from './../../providers/busiservice/busiservice';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic-angular';

/**
 * Generated class for the OrderaddPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-orderadd',
  templateUrl: 'orderadd.html',
})
export class OrderaddPage {
  title: string = "";
  des: string = "";
  type: string = "";
  loading: LoadingUtil;
  loading2: LoadingUtil;
  typelist = [];
  shoplist=[];
  selectedshop:any;
  constructor(public navCtrl: NavController, public navParams: NavParams, public busiservice: BusiserviceProvider, private loadingCtrl: LoadingController, private uiservice: UiserviceProvider) {
    this.loading = new LoadingUtil(this.loadingCtrl);
    this.loading2 = new LoadingUtil(this.loadingCtrl);
    this.loadOrderTypelist();
    this.getShopList();


  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad OrderaddPage');
  }
  getShopList(){
    this.loading2.showLoading();
    this.busiservice.getShopList().subscribe(
      data => {
        if (data._success) {
          this.shoplist = data.result;
          this.loading2.hideLoading();
          //用户id
        } else {
          this.loading2.hideLoading();
          this.uiservice.showAlert(data._message);
        }
      }
      ,
      error => {
        this.loading2.hideLoading();
        this.uiservice.showAlert("网络异常!");
      }
    );
  }
  loadOrderTypelist() {
    this.loading.showLoading();
    this.busiservice.getAllOrderType().subscribe(
      data => {
        if (data._success) {
          this.typelist = data.result;
          this.loading.hideLoading();
          //用户id
        } else {
          this.loading.hideLoading();
          this.uiservice.showAlert(data._message);
        }
      }
      ,
      error => {
        this.loading.hideLoading();
        this.uiservice.showAlert("网络异常!");
      }
    );
  }
  addOrder() {
    if (Utils.isNullOrEmpty(this.type)) {
      alert("类型为空！");
      return;
    }
    if (Utils.isNullOrEmpty(this.title)) {
      alert("标题为空！");
      return;
    }
    if (Utils.isNullOrEmpty(this.des)) {
      alert("详细描述为空！");
      return;
    }
    this.loading.showLoading();
    this.busiservice.addOrder(this.title, this.des, this.type,this.selectedshop).subscribe(
      data => {
        if (data._success) {
          this.loading.hideLoading();
          this.navCtrl.pop();
          //用户id
        } else {
          this.loading.hideLoading();
          this.uiservice.showAlert(data._message);
        }
      }
      ,
      error => {
        this.loading.hideLoading();
        this.uiservice.showAlert("网络异常!");
      }
    );

  }

}
